package cn.iocoder.yudao.module.promotion.redpacket.controller.admin;

import cn.iocoder.yudao.framework.business.basic.annotation.AdminJsonView;
import cn.iocoder.yudao.framework.business.basic.controller.PageController;
import cn.iocoder.yudao.framework.business.basic.controller.SimpleDetailController;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.ShopSimpleVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.promotion.redpacket.vo.RedPacketConfigVO;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.promotion.redpacket.convert.RedPacketConfigConvert;
import cn.iocoder.yudao.module.promotion.redpacket.pojo.dto.RedPacketConfigQueryDTO;
import cn.iocoder.yudao.module.promotion.redpacket.pojo.dto.RedPacketConfigSaveDTO;
import cn.iocoder.yudao.module.promotion.redpacket.pojo.po.RedPacketConfig;
import cn.iocoder.yudao.module.promotion.redpacket.service.admin.RedPacketConfigService;
import cn.iocoder.yudao.module.promotion.redpacket.service.core.CoreRedPacketConfigService;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 前端控制器
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/06/21
 */
@Tag(name = "管理后台 - 红包参数配置")
@RestController
@RequestMapping("/promotion/redpacket/config")
public class RedPacketConfigController implements SimpleDetailController<RedPacketConfigVO>, PageController<RedPacketConfigVO, RedPacketConfigQueryDTO> {
    @Resource
    private RedPacketConfigService redPacketConfigService;
    @Resource
    private CoreRedPacketConfigService coreRedPacketConfigService;

    @Resource
    private ShopInfoApi shopInfoApi;

    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin', 'shop_admin')")
    @JsonView(AdminJsonView.class)
    @Override
    public CommonResult<RedPacketConfigVO> getDetail(Long id) {
        return CommonResult.success(redPacketConfigService.getDetail(id));
    }

    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin', 'shop_admin')")
    @JsonView(AdminJsonView.class)
    @Override
    public CommonResult<PageResult<RedPacketConfigVO>> getPage(Integer pageNo, Integer pageSize, RedPacketConfigQueryDTO param) {
        PageResult<RedPacketConfigVO> page = redPacketConfigService.getPage(pageNo, pageSize, param);
        Set<Long> shopIds = convertSet(page.getList(), RedPacketConfigVO::getShopId);
        Map<Long, ShopSimpleVO> shops = shopInfoApi.getShopSimpleMap(shopIds);
        page.getList().forEach(vo -> vo.setShop(shops.getOrDefault(vo.getShopId(), ShopSimpleVO.EMPTY)));
        return CommonResult.success(page);
    }

    @GetMapping("/read")
    @Operation(summary = "读取配置内容")
    @PreAuthorize("@ss.hasRole('shop_admin')")
    @JsonView(AdminJsonView.class)
    public CommonResult<RedPacketConfigVO> read() {
        Long shopId = shopInfoApi.getShopIdByAdminUserId(getLoginUserId());
        RedPacketConfig config = coreRedPacketConfigService.getOrCreate(shopId);
        return CommonResult.success(RedPacketConfigConvert.INSTANCE.toViewObject(config));
    }

    @PostMapping("/write")
    @Operation(summary = "写入配置内容")
    @PreAuthorize("@ss.hasRole('shop_admin')")
    public CommonResult<Boolean> write(@RequestBody @Valid RedPacketConfigSaveDTO param) {
        Long shopId = shopInfoApi.getShopIdByAdminUserId(getLoginUserId());
        return CommonResult.success(redPacketConfigService.save(shopId, param));
    }

}
